#!/bin/bash

ROOT_PATH=$(cd $(dirname $0) && pwd);

apt purge -y softether-vpnclient
apt autoremove -y

apt -y install softether-vpnclient


PASS=$(cat $ROOT_PATH/cfg/user.secret)

sh -c "echo '
NicCreate prz

AccountCreate PRZ /SERVER:prz.geocat.space:443 /HUB:z /USERNAME:prx /NICNAME:prz
AccountPasswordSet PRZ /PASSWORD:'$PASS' /TYPE:standard
AccountStartupSet PRZ
AccountConnect PRZ

' > /tmp/softether.config"


sudo vpncmd localhost /CLIENT /in:/tmp/softether.config

#service softether-vpnserver restart

#rm /tmp/softether.config


if [ -z "$(sed -n '/vpn_prz/p' /etc/netplug/netplugd.conf)" ]
then

sudo sh -c "echo '
vpn_prz
' >> /etc/netplug/netplugd.conf"

fi


sh -c "echo '

allow-hotplug vpn_prz
iface vpn_prz inet dhcp
    post-up /sbin/ip route add default via 10.126.251.2 table 120
    post-up /sbin/ip rule add from 10.126.253.128/25 table 120
    post-down /sbin/ip rule delete from 10.126.253.128/25 table 120
    post-down /sbin/ip route delete default via 10.126.251.2 table 120

' > /etc/network/interfaces.d/vpn_prz"
